﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Zosnn.Models;

namespace Zosnn.Web.Controllers
{
    public class OrderInfoController : Controller
    {
        private ZosnnContext db = new ZosnnContext();

        // GET: /OrderInfo/
        public ActionResult Index()
        {
            var orderinfoes = db.OrderInfoes.Include(o => o.Order).Include(o => o.Product);
            return View(orderinfoes.ToList());
        }

        // GET: /OrderInfo/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderInfo orderinfo = db.OrderInfoes.Find(id);
            if (orderinfo == null)
            {
                return HttpNotFound();
            }
            return View(orderinfo);
        }

        // GET: /OrderInfo/Create
        public ActionResult Create()
        {
            ViewBag.OrderID = new SelectList(db.Orders, "ID", "Status");
            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name");
            return View();
        }

        // POST: /OrderInfo/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,ProductID,OrderID,Count,ProductName,SmallPic")] OrderInfo orderinfo)
        {
            if (ModelState.IsValid)
            {
                db.OrderInfoes.Add(orderinfo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.OrderID = new SelectList(db.Orders, "ID", "Status", orderinfo.OrderID);
            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", orderinfo.ProductID);
            return View(orderinfo);
        }

        // GET: /OrderInfo/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderInfo orderinfo = db.OrderInfoes.Find(id);
            if (orderinfo == null)
            {
                return HttpNotFound();
            }
            ViewBag.OrderID = new SelectList(db.Orders, "ID", "Status", orderinfo.OrderID);
            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", orderinfo.ProductID);
            return View(orderinfo);
        }

        // POST: /OrderInfo/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,ProductID,OrderID,Count,ProductName,SmallPic")] OrderInfo orderinfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(orderinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.OrderID = new SelectList(db.Orders, "ID", "Status", orderinfo.OrderID);
            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", orderinfo.ProductID);
            return View(orderinfo);
        }

        // GET: /OrderInfo/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderInfo orderinfo = db.OrderInfoes.Find(id);
            if (orderinfo == null)
            {
                return HttpNotFound();
            }
            return View(orderinfo);
        }

        // POST: /OrderInfo/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            OrderInfo orderinfo = db.OrderInfoes.Find(id);
            db.OrderInfoes.Remove(orderinfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
